; Create a GUI for the window list and resolution options
Gui, Add, ListBox, vWindowList gSelectWindow w300 h200
Gui, Add, ListBox, vResolutionList gSelectResolution w300 h100, 1920x1080|1768x992|1680x1050|1600x900|1440x1080|1440x900|1366x768|1280x1080|1280x1024|1280x960|1280x800|1280x768|1280x720|1024x768

;Gui, Add, Button, gRefreshList, &Refresh List  ; Alt+R for Refresh List
;Gui, Add, Button, gActivateWindow, &Activate Selected Window  ; Alt+A for Activate Selected Window
Gui, Add, Button, gApplyResolution, Apply &Resolution

Gui, Show,, Open Windows List and Resolution Options

; Variable to store the selected window handle
SelectedWindowHandle := ""

; Variable to store the selected resolution
SelectedResolution := ""

; Auto-select the first resolution in the list
GuiControl, Choose, ResolutionList, 1
; Trigger the SelectResolution subroutine to update SelectedResolution
Gosub, SelectResolution

; Populate the list with open windows
RefreshList:
    GuiControl,, WindowList, |  ; Clear the list
    WinGet, windows, List
    Loop, %windows%
    {
        WinGetClass, class, % "ahk_id " windows%A_Index%
        if class
            GuiControl,, WindowList, %class%
    }
    ;SelectedWindowHandle := "" ; Reset the selected window handle

    ; Auto-select the first window in the list
    GuiControl, Choose, WindowList, 1
    ; Trigger the SelectWindow subroutine to update SelectedWindowHandle
    Gosub, SelectWindow
    
return

; When a window is selected from the list
SelectWindow:
    Gui, Submit, NoHide
    ; Find the window handle based on the selected class
    WinGet, windows, List
    Loop, %windows%
    {
        WinGetClass, class, % "ahk_id " windows%A_Index%
        if (class = WindowList)
        {
            SelectedWindowHandle := windows%A_Index% ; Store the selected window handle
            break
        }
    }
return

; When a resolution is selected from the list
SelectResolution:
    Gui, Submit, NoHide
    SelectedResolution := ResolutionList ; Store the selected resolution
return

; Activate the selected window when the button is clicked
ActivateWindow:
    if SelectedWindowHandle
    {
        WinActivate, % "ahk_id " SelectedWindowHandle
    }
    else
    {
        MsgBox, No window selected!
    }
return

; Apply the selected resolution to the selected window
ApplyResolution:
    if !SelectedWindowHandle
    {
        MsgBox, No window selected!
        return
    }
    if !SelectedResolution
    {
        MsgBox, No resolution selected!
        return
    }

    ; Parse the selected resolution into width and height
    StringSplit, ResolutionParts, SelectedResolution, x
    Width := ResolutionParts1
    Height := ResolutionParts2

    newX := (A_ScreenWidth - Width) // 2
    newY := (A_ScreenHeight - Height) // 2

    ; Resize and move the window
    WinActivate, % "ahk_id " SelectedWindowHandle
    WinMove, % "ahk_id " SelectedWindowHandle, , newX, newY, %Width%, %Height%

    ;MsgBox, Resolution applied: %SelectedResolution%
return

; Close the GUI when the window is closed
GuiClose:
    ExitApp
return